/*
 *  Copyright (C) 2010 Martin Haulrich <mwh.isv@cbs.dk> and Matthias Buch-Kromann <mbk.isv@cbs.dk>
 *
 *  This file is part of the IncrementalParser package.
 *
 *  The IncrementalParser program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.osdtsystem.incparser.features;

import gnu.trove.TFloatArrayList;
import java.io.IOException;
import java.io.Serializable;
import java.io.Writer;

/**
 *
 * @author Matthias Buch-Kromann and Martin Haulrich
 */
public class FeatureVectorDense extends AbstractFeatureVector implements Serializable {
    TFloatArrayList weights = new TFloatArrayList();
    static int ids = 0;
    int id;
    
    public FeatureVectorDense(int initialCapacity) {
        this();
        weights.ensureCapacity(initialCapacity);
    }

    public FeatureVectorDense() {
        id = ids++;
    }

    public float weight(int feature) {
        return feature >= 0 && feature < weights.size()
            ? weights.get(feature)
            : 0;
    }

    public void setWeight(int feature, float weight) {
        if (feature >= weights.size()) {
            weights.ensureCapacity(feature + 1);
            weights.fill(weights.size(), feature + 1, 0);
        }
        weights.setQuick(feature, weight);
        // printDebug(feature,  weight);
    }

    void printDebug(int feature, float weight) {
        System.out.println("FVDense[" + id +
                "]: " + feature + "=" + weight);
    }

    public void addWeight(int feature, float weight) {
        float newWeight = weight(feature) + weight;
        setWeight(feature, newWeight);
    }

    @Override
    public String toString() {
        return weights.toString();
    }

    public int features() {
        return weights.size();
    }

    public void addTo(float scalarThis, FeatureVector other) {
        for (int feature = weights.size() - 1; feature >= 0; --feature) {
            other.addWeight(feature, weights.get(feature) * scalarThis);
        }
    }

    public void addTo(FeatureAggregator aggregator) {
        for (int feature = weights.size() - 1; feature >= 0; --feature) {
            aggregator.addFeature(feature, weights.get(feature));
        }
    }

    public void toSet() {
        //System.out.println("addTo[" + features() + ":" + other.features());
        for (int feature = weights.size() - 1; feature >= 0; --feature) {
            if (weights.get(feature) != 0)
                weights.set(feature, 1);
        }
    }
    
    public FeatureVector multiply(float scalar) {
        FeatureVector result = new FeatureVectorDense(weights.size());
        for (int feature = weights.size() - 1; feature >= 0; --feature) {
            result.setWeight(feature, weights.get(feature) * scalar);
        }
        return result;
    }

    public FeatureVector add(FeatureVector other) {
        if (other.features() > features())
            return other.add(this);
        FeatureVector result = this.clone();
        other.addTo(1, result);
        return result;
    }
    
    public FeatureVector subtract(FeatureVector other) {
        FeatureVector result = this.clone();
        other.addTo(-1, result);
        return result;
    }

    public FeatureVector linearCombination(float scaleThis, float scaleOther, FeatureVector other) {
        if (other.features() > features())
            return other.linearCombination(scaleOther, scaleThis, this);
        FeatureVector result = multiply(scaleThis);
        other.addTo(scaleOther, result);
        return result;
    }

    public double innerProduct(FeatureVector other) {
        // The smallest feature vector makes the computation
        if (other.features() < features())
            return other.innerProduct(this);

        // Compute inner product
        double innerProduct = 0;
        for (int feature = 0; feature < features(); ++feature) {
            innerProduct += weights.get(feature) * other.weight(feature);
        }

        // Return result
        return innerProduct;
    }

    @Override
    public FeatureVectorDense clone() {
        FeatureVectorDense clone = (FeatureVectorDense) super.clone();
        clone.weights = (TFloatArrayList) weights.clone();
        return clone;
    }

    public void clear() {
        weights.clear();
    }

    public void trimToSize() {
        weights.trimToSize();
    }

    @Override
    public void writeToFile(Writer writer) throws IOException {
        writer.write("fvdense #" + id);
        for (int feature = 0; feature < weights.size(); ++feature) {
            float weight = weights.get(feature);
            if (weight != 0) {
                writer.write(feature + "=" + String.format("%6g", weight) + "\n");
            }
        }
    }
}
